package com.atguigu.srb.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GatewayConfig {
    //跨域配置
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 是否允许携带cookie跨域访问
        config.addAllowedHeader("*"); // 允许携带哪些请求头跨域访问：* 通配所有
        config.addAllowedMethod("*"); //允许哪些请求方式跨域访问： * 通配所有
        config.addAllowedOrigin("*"); //允许哪些服务器跨域访问： * 通配所有
        source.registerCorsConfiguration("/**" , config);
        return new CorsWebFilter(source);
    }
}
